<template>
  <el-dialog
    :title="!houses.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="houses" :rules="dataRule" ref="houses" @keyup.enter.native="housesSubmit()" label-width="80px">
    <el-form-item label="省份" prop="province">
      <el-input v-model="houses.province" placeholder="省份"></el-input>
    </el-form-item>
    <el-form-item label="省份id" prop="provinceId">
      <el-input v-model="houses.provinceId" placeholder="省份id"></el-input>
    </el-form-item>
    <el-form-item label="启动城市" prop="startCity">
      <el-input v-model="houses.startCity" placeholder="启动城市"></el-input>
    </el-form-item>
    <el-form-item label="禁用城市" prop="disCity">
      <el-input v-model="houses.disCity" placeholder="禁用城市"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="housesSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        houses: {
          id: 0,
          province: '',
          provinceId: '',
          startCity: '',
          disCity: ''
        },
        dataRule: {
          province: [
            { required: true, message: '省份不能为空', trigger: 'blur' }
          ],
          provinceId: [
            { required: true, message: '省份id不能为空', trigger: 'blur' }
          ],
          startCity: [
            { required: true, message: '启动城市不能为空', trigger: 'blur' }
          ],
          disCity: [
            { required: true, message: '禁用城市不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.houses.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['houses'].resetFields()
          if (this.houses.id) {
            this.$http({
              url: this.$http.adornUrl(`/area/area/info/${this.houses.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.houses.province = data.area.province
                this.houses.provinceId = data.area.provinceId
                this.houses.startCity = data.area.startCity
                this.houses.disCity = data.area.disCity
              }
            })
          }
        })
      },
      // 表单提交
      housesSubmit () {
        this.$refs['houses'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/area/area/${!this.houses.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.houses.id || undefined,
                'province': this.houses.province,
                'provinceId': this.houses.provinceId,
                'startCity': this.houses.startCity,
                'disCity': this.houses.disCity
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
